package com.shsoft.component;

import com.shsoft.config.PCStatusConstant;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class ShapeComponent extends BaseComponent{

    private String shapeType;

    private int width;

    private int height;

    private int arc;

    private int lineWidth;

    private double colorR,colorG,colorB,colorA;

    @Override
    public void drawComponent(GraphicsContext gc) {
        gc.setLineWidth(lineWidth);
        gc.setStroke(getColor());
        gc.setFill(getColor());
        if(shapeType.equals(PCStatusConstant.SHAPE_LINE)){
            gc.strokeLine(getX(),getY(),width,height);
        }else if(shapeType.equals(PCStatusConstant.SHAPE_STROKE_RECT)){
            gc.strokeRoundRect(getX(),getY(),width,height,arc,arc);
        }else if (shapeType.equals(PCStatusConstant.SHAPE_FILL_RECT)){
            gc.fillRoundRect(getX(),getY(),width,height,arc,arc);
        }else if(shapeType.equals(PCStatusConstant.SHAPE_STROKE_OVAL)){
            gc.strokeOval(getX(),getY(),width,height);
        }else if(shapeType.equals(PCStatusConstant.SHAPE_FILL_OVAL)){
            gc.fillOval(getX(),getY(),width,height);
        }
        gc.setLineWidth(1);
    }

    @Override
    public void drawComponentBorder(Canvas canvas) {
        GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.setStroke(Color.GREY);
        if(shapeType.equals(PCStatusConstant.SHAPE_LINE)){
            int x1,x2,y1,y2;
            if(width >= getX()){
                x1 = getX();
                x2 = width;
            }else {
                x1 = width;
                x2 = getX();
            }
            if(height >= getY()){
                y1 = getY();
                y2 = height;
            }else {
                y1 = height;
                y2 = getY();
            }
            gc.strokeRect(x1 - 2 - lineWidth / 2,y1 - 2 - lineWidth / 2, x2 - x1 + 4 + lineWidth, y2 - y1 + 4 + lineWidth);
        }else {
            gc.strokeRect(getX() - 2 - lineWidth / 2, getY() - 2 - lineWidth / 2, width + 4 + lineWidth, height + 4 + lineWidth);
        }
    }

    @Override
    public boolean isMe(double x, double y) {
        if(shapeType.equals(PCStatusConstant.SHAPE_LINE)){
            int x1,x2,y1,y2;
            if(width >= getX()){
                x1 = getX();
                x2 = width;
            }else {
                x1 = width;
                x2 = getX();
            }
            if(height >= getY()){
                y1 = getY();
                y2 = height;
            }else {
                y1 = height;
                y2 = getY();
            }
            if(x >= x1 - 2 - lineWidth / 2 && x <= x2 + 4 + lineWidth / 2 && y >= y1 - 2 - lineWidth / 2 && y <= y2 + 4 + lineWidth / 2){
                return true;
            }
            return false;
        }else {
            if(x >= getX() - 2 - lineWidth / 2 && x <= getX() + width + 4 + lineWidth / 2 && y >= getY() - 2 - lineWidth / 2 && y <= getY() + height + 4 + lineWidth / 2){
                return true;
            }
            return false;
        }
    }

    public void setColor(Color color) {
        this.colorR = color.getRed();
        this.colorG = color.getGreen();
        this.colorB = color.getBlue();
        this.colorA = color.getOpacity();
    }

    public Color getColor() {
        return new Color(colorR,colorG,colorB,colorA);
    }

    public int getArc() {
        return arc;
    }

    public void setArc(int arc) {
        this.arc = arc;
    }

    public int getLineWidth() {
        return lineWidth;
    }

    public void setLineWidth(int lineWidth) {
        this.lineWidth = lineWidth;
    }

    public String getShapeType() {
        return shapeType;
    }

    public void setShapeType(String shapeType) {
        this.shapeType = shapeType;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

}
